﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;	// Using Win8 specific extension methods
    using System.Linq;
    using System.Reflection;

    public static class DemoableHelper
	{        
        public static bool IsDemoableClass(Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();
			return typeInfo.IsClass && typeInfo.IsVisible && !typeInfo.IsAbstract && typeInfo.IsDefined(typeof(EditorBrowsableAttribute));
        }			

		public static IEnumerable<Type> DemoableClasses(Assembly assembly)
		{
			foreach (Type type in assembly.ExportedTypes.Where (t => IsDemoableClass(t)))
			{
                yield return type;
			}
		}

		public static IEnumerable<PropertyInfo> DemoableProperties(Type classType)
		{
			foreach (PropertyInfo pi in classType.GetRuntimeProperties())
			{
				if (pi.CanRead)
				{
                    EditorBrowsableAttribute attribute = (EditorBrowsableAttribute) pi.GetCustomAttribute(typeof(EditorBrowsableAttribute));
                    if (attribute != null && attribute.State != EditorBrowsableState.Never)
                    {
                        yield return pi;
                    }
				}
			}
		}
	}
}
